<script setup lang="ts">
import {onBeforeUnmount, ref, watch} from "vue";
import {useCategoryStore} from "@/stores/modules/category.ts";
import {reqDeleteSpu, reqHasSpu, reqSkuList} from "@/api/product/spu";
import type {HasSpuResponseData, Records, SkuData, SkuInfoData} from "@/api/product/spu/type.ts";
import SpuForm from "@/views/product/spu/spuForm.vue";
import SkuForm from "@/views/product/spu/skuForm.vue";
import type {SpuData} from "@/api/product/spu/type.ts";
import {ElMessage} from "element-plus";

/*
* 0显示已有SPU
* 1添加或修改已有SPU
* */
const scene = ref<boolean>(true)
const sceneChange = ref<number>(0)
const pageNum = ref<number>(1)
const pageSize = ref<number>(3)
const records = ref<Records>([])
const categoryStore = useCategoryStore()
const total = ref<number>(0)
const SpuCom = ref()
const SkuCom = ref()
const dialogVisible = ref<boolean>(false)
const skuArr = ref<Array<SkuData>>([])

watch(()=>categoryStore.C3Id, ()=>{
  // 如果没有三级分类，就不发送请求获取spu
  if(!categoryStore.C3Id) return
  getHasSpu()
})

const getHasSpu = async (pager = 1) => {
  console.log(pager)
  pageNum.value = pager
  const result:HasSpuResponseData = await reqHasSpu(pageNum.value, pageSize.value, categoryStore.C3Id)
  if(result.code === 200) {
    records.value = result.data.records
    total.value = result.data.total
    console.log("this is spu spu", result)
  }
}

const changeSize = () => {
  getHasSpu()
}

const addSpu = () => {
  sceneChange.value = 1
  SpuCom.value.initAddSpu(categoryStore.C3Id)
}

const updateSpu = (row:SpuData) => {
  sceneChange.value = 1
  SpuCom.value.initHasSpuData(row)
}

const changeScene = (obj:any) => {
  sceneChange.value = obj.flag
  if(obj.params === 'update') {
    getHasSpu(pageNum.value)
  }else {
    getHasSpu()
  }
}

const addSku = (row:SpuData) => {
  SkuCom.value.initSkuData(categoryStore.C1Id, categoryStore.C2Id, row)
  sceneChange.value = 2
}

const skuView = async (row:SpuData) => {
  const result:SkuInfoData = await reqSkuList(row.id as number)
  if(result.code === 200) {
    skuArr.value = result.data
    dialogVisible.value = true
  }
}

const deleteSpu = async (row:SpuData) => {
  const result = await reqDeleteSpu(row.id as number)
  if(result.code === 200) {
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    await getHasSpu(records.value.length>1?pageNum.value:pageNum.value-1)
  }else {
    ElMessage({
      type:'error',
      message:'删除失败'
    })
  }
}
onBeforeUnmount(()=>{
  categoryStore.$reset()
})
</script>

<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <div v-show="sceneChange === 0">
        <el-button type="primary" @click="addSpu" :disabled="!categoryStore.C3Id" size="default" icon="Plus">添加SPU</el-button>
        <el-table style="margin: 10px 0" border :data="records">
          <el-table-column label="序号" type="index" width="80px" align="center"></el-table-column>
          <el-table-column label="SPU名称" align="center" prop="spuName"></el-table-column>
          <el-table-column show-overflow-tooltip label="SPU描述" align="center" prop="description"></el-table-column>
          <el-table-column label="SPU操作" align="center">
            <template v-slot="{row}">
              <el-button type="primary" @click="addSku(row)" size="default" icon="Plus" title="添加SKU"></el-button>
              <el-button type="warning" @click="updateSpu(row)" size="default" icon="Edit" title="修改SPU"></el-button>
              <el-button @click="skuView(row)" type="info" size="default" icon="View" title="查看SKU"></el-button>
              <el-popconfirm @confirm="deleteSpu(row)" :title="`你确定要删除${row.spuName}吗`" style="width: 200px">
                <template v-slot:reference>
                  <el-button type="danger" size="default" icon="Delete" title="删除SPU"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="pageNum"
            v-model:page-size="pageSize"
            :page-sizes="[3,5,7,9]"
            :background="true"
            layout="prev, pager, next, jumper, ->, sizes, total"
            :total="total"
            @current-change="getHasSpu"
            @size-change="changeSize"/>
      </div>
      <SpuForm ref="SpuCom" v-show="sceneChange === 1" @change-scene="changeScene"/>
      <SkuForm ref="SkuCom" @change-scene="changeScene" v-show="sceneChange === 2"/>
      <el-dialog v-model="dialogVisible" title="SKU列表">
        <el-table :data="skuArr" border>
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template v-slot="{row}">
              <img :src="row.skuDefaultImg" style="width: 100px;height: 100%">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<style scoped lang="scss">

</style>